package com.example.demo.controller;


import com.example.demo.entity.*;
import com.example.demo.service.VideoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class VideoController {
    @Autowired
    private VideoService videoService;

    @CrossOrigin
    @GetMapping(value = "/api/listVideo")
    @ResponseBody
    public Result listVideo()
    {

        List<Video> videos = videoService.findAllVideo();
        Result result = new Result(200);
        result.setData(videos);
        return result;
    }


    @CrossOrigin
    @GetMapping(value = "/api/listVideoBySearchMsg")
    @ResponseBody
    public Result listVideoBySearchMsg(@RequestParam("searchMsg") String searchMsg,
                                       @RequestParam("currentPage") int currentPage)
    {
        List<Video> videos = videoService.listVideoBySearchMsg(searchMsg,currentPage);
        int total = videoService.listVideoBySearchMsgTotal(searchMsg);
        Result result = new Result(200);
        result.setData(videos);
        result.setTotal(total);
        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/deleteVideo")
    @ResponseBody
    public void deleteVideo(@RequestParam("id") int id)
    {
        videoService.deleteVideoById(id);
    }

    @CrossOrigin
    @PostMapping(value = "/api/editVideo")
    @ResponseBody
    public void editVideo(@RequestBody Video video)
    {
        videoService.editVideo(video);
    }

    @CrossOrigin
    @PostMapping(value = "/api/addVideo")
    @ResponseBody
    public void addVideo(@RequestBody Video video)
    {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间，也可使用当前时间戳
        video.setTime(date);
        videoService.addVideo(video);
    }


    @CrossOrigin
    @GetMapping(value = "/api/findVideoById")
    @ResponseBody
    public Result findVideoById(@RequestBody int goodsId)
    {

        Result result = new Result(200);

        return result;
    }

    @CrossOrigin
    @GetMapping(value = "/api/addOrder")
    @ResponseBody
    public Result insertNewOrder(@RequestBody Order requestOrder)
    {

        Result result = new Result(200);

        return result;
    }

}
